package com.swust.course.controller;

import com.swust.common.core.utils.poi.ExcelUtil;
import com.swust.common.core.web.controller.BaseController;
import com.swust.common.core.web.domain.AjaxResult;
import com.swust.common.core.web.page.TableDataInfo;
import com.swust.common.log.annotation.Log;
import com.swust.common.log.enums.BusinessType;
import com.swust.common.security.annotation.RequiresPermissions;
import com.swust.common.security.utils.SecurityUtils;
import com.swust.course.domain.CourseInfo;
import com.swust.course.service.ICourseInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 课程信息Controller
 *
 * @author LiuShuai
 * @date 2024-04-04
 */
@RestController
@RequestMapping("/courseInfo")
public class CourseInfoController extends BaseController {
    @Autowired
    private ICourseInfoService courseInfoService;

    /**
     * 课程数据导入模板
     *
     * @param response
     * @throws IOException
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil<CourseInfo> util = new ExcelUtil<>(CourseInfo.class);
        util.importTemplateExcel(response, "课程数据");
    }


    @Log(title = "课程信息管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("course:courseInfo:export")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<CourseInfo> util = new ExcelUtil<>(CourseInfo.class);
        final List<CourseInfo> courseInfos = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = courseInfoService.importCourseInfo(courseInfos, updateSupport, operName);
        return success(message);
    }

    /**
     * 查询课程信息列表
     */
    @RequiresPermissions("course:courseInfo:list")
    @GetMapping("/list")
    public TableDataInfo list(CourseInfo courseInfo) {
        startPage();
        List<CourseInfo> list = courseInfoService.selectCourseInfoList(courseInfo);
        return getDataTable(list);
    }

    /**
     * 导出课程信息列表
     */
    @RequiresPermissions("course:courseInfo:export")
    @Log(title = "课程信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CourseInfo courseInfo) {
        List<CourseInfo> list = courseInfoService.selectCourseInfoList(courseInfo);
        ExcelUtil<CourseInfo> util = new ExcelUtil<CourseInfo>(CourseInfo.class);
        util.exportExcel(response, list, "课程信息数据");
    }

    /**
     * 获取课程信息详细信息
     */
    @RequiresPermissions("course:courseInfo:query")
    @GetMapping(value = "/{courseCode}")
    public AjaxResult getInfo(@PathVariable("courseCode") String courseCode) {
        return success(courseInfoService.selectCourseInfoByCourseCode(courseCode));
    }

    /**
     * 新增课程信息
     */
    @RequiresPermissions("course:courseInfo:add")
    @Log(title = "课程信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CourseInfo courseInfo) {
        return toAjax(courseInfoService.insertCourseInfo(courseInfo));
    }

    /**
     * 修改课程信息
     */
    @RequiresPermissions("course:courseInfo:edit")
    @Log(title = "课程信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CourseInfo courseInfo) {
        return toAjax(courseInfoService.updateCourseInfo(courseInfo));
    }

    /**
     * 删除课程信息
     */
    @RequiresPermissions("course:courseInfo:remove")
    @Log(title = "课程信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{courseCodes}")
    public AjaxResult remove(@PathVariable String[] courseCodes) {
        return toAjax(courseInfoService.deleteCourseInfoByCourseCodes(courseCodes));
    }
}
